﻿<div style="width: 100%;">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a class="mini-button" iconcls="icon-save" onclick="saveData();" plain="true">保存</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-node" onclick="addRootNode();" plain="true">创建层级树</a>
                    <!--<span class="separator"></span>
                    <a class="mini-button" iconcls="icon-cut" onclick="copyNode()" plain="true">复制节点</a>-->
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="mini-fit">
    <div id="treegrid" class="mini-treegrid" style="width: 100%; height: 100%;" expandonnodeclick="false" url="/SystemData/HierarchyTree/GetAllHierarchyTreeList"
        treecolumn="DKeyName" idfield="HID" parentfield="ParentID" resultastree="false" multiselect="true" contextmenu="#treeMenu"
        allowresize="false" showtreelines="true" expandonload="false" allowcelledit="true" allowcellvalid="true" allowcellselect="true">
        <div property="columns">
            <div type="indexcolumn" headeralign="center" align="center" width="20" name="indexcolumn">序号</div>

            <div field="DName" width="100" headeralign="center">
                层级树类型
            </div>
            <div type="checkcolumn" headeralign="center" align="center" width="10">&nbsp;</div>
            <div name="DKeyName" field="DKeyName" width="180" headeralign="center">
                层级名称  
            </div>
            <div field="DKey" width="50" name="Code" headeralign="center">
                层级项目编码
            </div>
            <div name="HLevel" field="HLevel" width="30" headeralign="center" align="right">
                当前层级
            </div>
            <div field="HSort" vtype="required" width="30" headeralign="center" align="right">
                排序
            <input property="editor" class="mini-spinner" minivalue="1" style="width: 100%;" />
            </div>
        </div>
    </div>
    <ul id="treeMenu" class="mini-contextmenu" onbeforeopen="onBeforeOpen">
        <li name="addleaf" iconcls="icon-add" onclick="addLeafNode">插入子节点</li>
        <!--<li name="paste" iconcls="icon-download">粘贴</li>-->
        <li name="remove" iconcls="icon-remove" onclick="deleteNode">删除节点</li>
    </ul>
</div>

<script type="text/javascript">
    mini.parse();

    var treegrid = mini.get("treegrid");

    function addRootNode() {
        var nodesCnt = treegrid.getData().length;
        mini.open({
            url: "/SystemData/HierarchyTree/AddRootNode",
            title: "添加根节点",
            width: 550,
            height: 390,
            allowResize: false,
            allowDrag: true,
            ondestroy: function (action) {
                if (action == "ok") {
                    var iframe = this.getIFrameEl();
                    var rows = iframe.contentWindow.getSelectedRows();
                    rows = mini.clone(rows);
                    if (rows) {
                        for (var i = 0, l = rows.length; i < l; i++) {
                            var row = {
                                HID: rows[i].HID,
                                DID: rows[i].DID,
                                HLevel: 1,
                                DName: rows[i].DKeyName,
                                DKey: rows[i].DKey,
                                DKeyName: rows[i].DKeyName,
                                HSort: nodesCnt + 1,
                                //ParentID: null,
                                IsRoot: true,
                                IsLeaf: true
                            };
                            nodesCnt = nodesCnt + 1;
                            treegrid.addNode(row, "after");
                        }
                    }
                }
            }
        });
    }

    function addLeafNode() {
        mini.open({
            url: "/SystemData/HierarchyTree/AddLeafNode",
            title: "添加子节点",
            width: 1024,
            height: 600,
            allowResize: false,
            allowDrag: true,
            ondestroy: function (action) {
                if (action == "ok") {
                    var iframe = this.getIFrameEl();
                    var data = iframe.contentWindow.getData();
                    var node = treegrid.getSelectedNode();
                    var childNodesCnt = treegrid.getChildNodes(node).length;
                    node.IsLeaf = false;

                    for (var i = 0, l = data.length; i < l; i++) {
                        var row = {
                            HID: data[i].HID,
                            DID: node.DID,
                            HLevel: node.HLevel + 1,
                            DName: node.DName,
                            DKey: data[i].DKey,
                            DKeyName: data[i].DValue,
                            HSort: childNodesCnt + 1,
                            ParentID: node.HID,
                            IsRoot: false,
                            IsLeaf: true
                        };
                        childNodesCnt = childNodesCnt + 1;
                        treegrid.addNode(row, "add", node);
                    }
                } else {
                    $.tip({ text: "取消操作" });
                }
            }
        });
    }

    function deleteNode() {
        var nodes = treegrid.getSelecteds();
        var message = "确定删除吗?";
        mini.showMessageBox({
            showModal: true,
            width: 500,
            height: 120,
            title: "提示",
            iconCls: "mini-messagebox-question",
            message: message,
            buttons: ["确定", "取消"],
            x: "center",
            y: "middle",
            callback: function (action) {
                if (action == "确定") {
                    for (var i = 0, l = nodes.length; i < l; i++) {
                        var pnode = treegrid.getParentNode(nodes[i]);
                        if (treegrid.getChildNodes(pnode).length == 0) {
                            pnode.IsLeaf = true;
                        }
                        treegrid.removeNode(nodes[i]);
                    }
                }
            }
        });
    }

    function copyNode() {

    }

    function saveData() {
        treegrid.loading("正在保存");
        var data = treegrid.getChanges();
        if (data.length <= 0) {
            treegrid.unmask();
            $.tip({ text: "未做任何修改", staytime: 1000 });
            return;
        }

        var add = treegrid.getChanges("added");
        add = mini.encode(add);
        var edit = treegrid.getChanges("modified");
        edit = mini.encode(edit);
        var remove = treegrid.getChanges("removed");
        remove = mini.encode(remove);

        $.ajax({
            url: "/SystemData/HierarchyTree/SaveData",
            data: { add: add, edit: edit, remove: remove },
            type: "post",
            success: function (text, flag, res) {
                treegrid.unmask();
                if (res.responseText == "") {
                    $.tip({ text: "保存成功", staytime: 1000 });
                } else {
                    mini.alert(res.responseText);
                }
            }
        });
    }

    //右键点击前
    function onBeforeOpen(e) {
        var menu = e.sender;

        var node = treegrid.getSelectedNode();

        if (!node) {
            e.cancel = true;
            return;
        }
        if (node && node.text == "Base") {
            e.cancel = true;
            e.htmlEvent.preventDefault();
            return;
        }
        var removeItem = mini.getbyName("remove", menu);
        removeItem.enable();
        if (node.id == "forms") {
            editItem.hide();
        }
        if (node.id == "lists") {
            removeItem.disable();
        }
    }
</script>
